﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataTier;
using DataAccessLayer;
using System.Collections;
using BusinessTier;


namespace GUILayer
{

    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
            dateTimePicker4.Format = DateTimePickerFormat.Custom;
            dateTimePicker4.CustomFormat = "HH mm";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //IList customers = DBCustomer.GetCustomers().ToArray();
            richTextBox1.Text = String.Join(Environment.NewLine, DBCustomer.GetCustomers());

        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = String.Join(Environment.NewLine, DBCustomer.GetCustomerByLoginEmail(emailBox.Text.ToString()));
            //richTextBox1.Text = String.Join(Environment.NewLine, DBCustomer.GetCustomerSubscriptions(1));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DBCustomer.CreateCustomer("test1", "Testaben", "Halsvej 2", 9700, "hej@hej.dk", "1234");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DBCustomer.DeleteCustomerByEmail("hej@hej.dk");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            List<Subscription> s = DBSubscription.GetSubscriptions(1);
            //Subscription sub = s[0];
            foreach (Subscription sub in s)
            {
                //if (richTextBox1.TextLength == 0)
                //{
                //richTextBox1.Text = String.Join(Environment.NewLine, sub.SubscriptionId, sub.SubscriptionType.Name, sub.CarModel.Description);
                //}
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, sub.SubscriptionId, Environment.NewLine, sub.SubscriptionType.Name, Environment.NewLine, sub.CarModel.Description);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = String.Join(Environment.NewLine, DBStation.GetAllStations());
            List<Station> stations = DBStation.GetAllStations();
            foreach (Station station in stations)
            {
                richTextBox1.Text += String.Join(Environment.NewLine, station.Edges.Count);
            }
        }

        private void stationBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            stationBox.Text = DBStation.GetAllStations().ToString();

        }

        private void button7_Click(object sender, EventArgs e)
        {
            List<Battery> b = DBBattery.GetBatteriesInStation(1);
            foreach (Battery bat in b)

                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, bat.BatteryId, bat.PercentCharged);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = String.Join(Environment.NewLine, DBSubscription.GetBatteriesInSubscription(2));

        }

        private void button9_Click(object sender, EventArgs e)
        {
            DBCustomer.UpdateCustomer(1, "lanzarote hans", "Christian Matthias Danielsen", "Ryesgade36", 9000, "Christiand999@hotmail.com", "1234");
            richTextBox1.Text = String.Join(Environment.NewLine, DBCustomer.GetCustomerByLoginEmail("christiand999@hotmail.com"));

        }

        private void button10_Click(object sender, EventArgs e)
        {
            CarModel m = DBBattery.GetCarModel(1);
            richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, m.BatteryModel, Environment.NewLine, m.Description, Environment.NewLine, m.KmOnFullCharge);

        }

        private void button11_Click(object sender, EventArgs e)
        {
            DBBattery.CreateBattery(1);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            DBSubscription.CreateSubscription(1, 1, 1, 1, 1);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            DateTime d = new DateTime();
            DBBattery.UpdateDateInBattery(1, d);
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "dd mm yyyy hh mm ss";
            DateTime result = dateTimePicker1.Value;
            DBBattery.UpdateDateInBattery(1, result);
            this.Text = result.ToString();

        }


        //private void button14_Click(object sender, EventArgs e)
        //{
        //   // BBooking b = new BBooking();
        //    Customer c = new Customer("1234");
        //    Subscription s = new Subscription();
        //    c.AddSubscription(s);

        //    List<Station> st = new List<Station>();
        //   // st = b.createBookingsOnRoute(9300, 7100, c, s);

        //    foreach (Station sta in st)
        //    {
        //        richTextBox1.Text += (st);
        //    }

        //}

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker2_ValueChanged_1(object sender, EventArgs e)
        {



        }

        private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
        {



        }

        private void button15_Click(object sender, EventArgs e)
        {

            DateTime d = DateTime.Now;

            Battery b = DBBattery.GetAvailableBattery(2, d);


            if (b != null)
            {
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, b.BatteryId);
            }
            else
            {
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, 000);
            }

        }

        //private void button16_Click(object sender, EventArgs e)
        //{
        //    DateTime d = DateTime.Now;
        //    d.AddYears(-10);
        //    List<Booking> b = DBBooking.FindBookingTimeInteval(d,);
        //    //Subscription sub = s[0];
        //    foreach (Booking book in b)
        //    {
        //        //if (richTextBox1.TextLength == 0)
        //        //{
        //        //richTextBox1.Text = String.Join(Environment.NewLine, sub.SubscriptionId, sub.SubscriptionType.Name, sub.CarModel.Description);
        //        //}
        //        richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, book.BookingId, Environment.NewLine, book.IsCancelled, Environment.NewLine);
        //    }
        //}

        private void button17_Click(object sender, EventArgs e)
        {
            List<Station> stations = DBStation.GetAllStations();
            foreach (Station s in stations)
            {
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, s.City, s.ZipCode);
                foreach (Edge edge in s.Edges)
                {
                    richTextBox1.Text += String.Join(Environment.NewLine, edge.To.City);
                }


            }


        }

        private void button18_Click(object sender, EventArgs e)
        {
            //richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, DBStation.GetAllEdges(1));

            List<Edge> edges = DBStation.GetAllEdges();

            foreach (Edge edge in edges)
            {
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, edge.From, edge.To, edge.Cost);
            }
            //BStation bStation = new BStation();

            //List<Station> stations = bStation.GetAllStations();
            //foreach (Station station in stations)
            //{
            //    richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, station.StationName, station.Edges.Count);
            //}

        }

        private void runRoute(object sender, EventArgs e)
        {
            IBBooking bBooking = new BBooking();
            Station s = bBooking.FindShortestRoute(9300, 6705);



            foreach (Station station in s.Connection)
            {

                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, station.City, station.PathSum);
            }




        }

        //private void findBookingTimeInteval(object sender, EventArgs e)
        //{
        //    DateTime dateTime = DateTime.Now;

        //    Booking booking = new Booking(dateTime);

        //    IBBooking bBooking = new BBooking();

        //    IBStation bStation = new BStation();

        //    List<Station> stations =  bStation.GetAllStations();

        //    richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, booking.BookingDate);

        //    richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, bBooking.FindBookingTimeInterval(booking, stations[5]));
        //}

        private void CalculateRoute(object sender, EventArgs e)
        {
            IBBooking bBooking = new BBooking();
            Customer customer = DBCustomer.GetCustomerByEmail("cbjerg@gmail.com");
            List<Subscription> subscriptions = DBSubscription.GetSubscriptions(customer.CustomerId);
            DateTime dateTime = DateTime.Now;

            List<Station> stations = DBStation.GetAllStations();

            Route route = bBooking.CalculateRoute(9300, "Brovst", "Hovengen 9", true, dateTime, 6705, "Esbjerg", "Himmelvej 2", customer, subscriptions[0]);

            richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, route.RouteId, route.FromCity, route.ToCity);
            foreach (Booking booking in route.Bookings)
            {
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, booking.BookingId, booking.BookingDate);

                foreach (Station station in stations)
                {
                    foreach (Battery battery in station.Batteries)
                    {
                        if (battery.BatteryId == booking.Battery.BatteryId)
                        {
                            richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, station.City);
                        }
                    }
                }



            }

            bBooking.AcceptRoute(route, customer, subscriptions[0]);




        }

        private void button23_Click(object sender, EventArgs e)
        {
            List<Booking> bookings = DBBooking.getAllBookings(1);
            foreach (Booking b in bookings)
            {
                richTextBox1.Text += String.Join(Environment.NewLine, Environment.NewLine, b.BookingId, Environment.NewLine, b.Subscription.SubscriptionId);

            }
        }
    }
}